package br.com.ordemservico.control;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;
import javax.swing.JOptionPane;

import br.com.ordemservico.control.conexao.Conexao;
import br.com.ordemservico.model.Cliente;
import br.com.ordemservico.model.Veiculo;
import br.com.ordemservico.view.ClienteView;

public class ClienteDao {
	Conexao con;
	public ClienteDao(){
		con = new Conexao();
		
	}
	

	public Cliente criar(Cliente c) {
		
		try{
			con.conectar();
			con.getTransaction().begin();
			con.getEntityManager().persist(c);
			con.getTransaction().commit();
			
		}catch(Exception e){
			System.out.println("Erro ao cadastrar cliente");
			e.getStackTrace();
		}finally{
			con.getEntityManager().close();
		}
		
		return c;
	}

	
	public Cliente editar(Cliente c) {
		
		try{
			con.conectar();
			con.getTransaction().begin();
			con.getEntityManager().merge(c);
			con.getTransaction().commit();
			
		}catch(Exception e){
			System.out.println("Erro ao editar cliente");
			e.printStackTrace();
		}finally{
			con.getEntityManager().close();
		}
		
		return c;
	}

	public Cliente findId(long id){
		Cliente c = null;
		try{
			con.conectar();
			con.getTransaction().begin();
			con.getTransaction().commit();
			c = (Cliente) con.getEntityManager().find(Cliente.class, id);
		}catch(Exception e){
			
		}finally{
			con.getEntityManager().close();
		}
		
		return c;
		
	}
	public boolean excluir(Cliente c) {
		//Cliente cliente = findId(c.getId_cliente());
		boolean r = true;
		try{
			con.conectar();
			con.getTransaction().begin();
			Cliente cliente = con.getEntityManager().merge(c);
			con.getEntityManager().remove(cliente);
			con.getTransaction().commit();
			
		}catch(Exception e){
			r = false;
			System.out.println("Erro ao excluir cliente");
			e.printStackTrace();
		}finally{
			con.getEntityManager().close();
		}
		
		return r;
		
	}
	public boolean verificarCpf(String cpf){
		Cliente c = null;
		boolean r = false;

		try{
			con.conectar();
			con.getTransaction().begin();
			Query query = con.getEntityManager().createNativeQuery("select * from cliente where cpf = ? ", Cliente.class).setParameter("1",cpf);
			con.getTransaction().commit();
			c =    (Cliente) query.getSingleResult();

		}catch(NoResultException e){
			r = true;
		}catch(Exception e){
			System.out.println("Erro ao pesqusiar ");
			e.printStackTrace();
		}finally{
			con.getEntityManager().close();
		}
		return r;
	}
	
	public List<Cliente> findAll() {
		List<Cliente> listaCliente = null;
		try{
			con.conectar();
			con.getTransaction().begin();
			Query query = con.getEntityManager().createNamedQuery("Cliente.findAll");
			con.getTransaction().commit();
			listaCliente =   query.getResultList();
		}catch(Exception e){
			System.out.println("Erro ao pesqusiar "+e.getStackTrace());
		}finally{
			con.getEntityManager().close();
		}
		return listaCliente;
	}
	public List<Cliente> findNome(String nome) {
		List<Cliente> listaCliente = null;
		try{
			con.conectar();
			con.getTransaction().begin();
			//Query query = con.getEntityManager().createNamedQuery("Cliente.findNome").setParameter("nome", nome);
			Query query = con.getEntityManager().createNativeQuery("select * from Cliente where nome like ? ", Cliente.class).setParameter("1", "%"+nome+"%");
			con.getTransaction().commit();
			listaCliente =  query.getResultList();

		}catch(Exception e){
			System.out.println("Erro ao pesqusiar "+e.getStackTrace());
		}finally{
			con.getEntityManager().close();
		}
		return listaCliente;
	}
	public List<Cliente> findListaCpf(String cpf) {
		List<Cliente> listaCliente = null;
		try{
			con.conectar();
			con.getTransaction().begin();
			//Query query = con.getEntityManager().createNamedQuery("Cliente.findNome").setParameter("nome", nome);
			Query query = con.getEntityManager().createNativeQuery("select * from Cliente where cpf like ? ", Cliente.class).setParameter("1", "%"+cpf+"%");
			con.getTransaction().commit();
			listaCliente =  query.getResultList();

		}catch(Exception e){
			System.out.println("Erro ao pesqusiar "+e.getStackTrace());
		}finally{
			con.getEntityManager().close();
		}
		return listaCliente;
	}
	public Cliente findCpf(String cpf) {
		Cliente cliente = null;
		try{
			con.conectar();
			con.getTransaction().begin();
			//Query query = con.getEntityManager().createNamedQuery("Cliente.findNome").setParameter("nome", nome);
			Query query = con.getEntityManager().createNativeQuery("select * from Cliente where cpf = ? ", Cliente.class).setParameter("1", cpf);
			con.getTransaction().commit();
			cliente =  (Cliente) query.getSingleResult();
		}catch(javax.persistence.NoResultException e){
			cliente = null;
		}catch(Exception e){
			System.out.println("Erro ao pesqusiar ");
			e.printStackTrace();
		}finally{
			con.getEntityManager().close();
		}
		return cliente;
	}

}
